package com.danger.Activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Presenter.SearchLostPresenter;
import com.danger.Presenter.SearchLostPresenterImpl;
import com.danger.View.adapter.MyAdapter;
import com.danger.Widget.MyScrollView;

import java.util.List;

public class SearchActivity extends AppCompatActivity implements com.danger.View.SearchView, MyScrollView.OnScrollListener {

    private Toolbar search_toolbar;
    private SearchView search_edit;
    private MyScrollView myScrollView;
    private int searchLayoutTop;

    LinearLayout search01, search02;
    RelativeLayout rlayout;
    private ProgressDialog dialog;
    private ListView search_listView;
    private SearchLostPresenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();
        initEvent();
    }

    private void initView() {
        search_toolbar = (Toolbar) findViewById(R.id.search_toolbar);
        search_toolbar.setTitle("搜索");
        search_toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(search_toolbar);
        search_edit = (SearchView) findViewById(R.id.search_edit);
        myScrollView = (MyScrollView) findViewById(R.id.myScrollView);
        search01 = (LinearLayout) findViewById(R.id.search01);
        search02 = (LinearLayout) findViewById(R.id.search02);
        rlayout = (RelativeLayout) findViewById(R.id.rlayout);
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在查询...");
        search_listView = (ListView) findViewById(R.id.search_listView);
        presenter = new SearchLostPresenterImpl(this, this);
    }

    private void initEvent() {
        search_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        search_edit.setIconifiedByDefault(false);
        myScrollView.setOnScrollListener(this);
        search_edit.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                presenter.searchByCategory(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            searchLayoutTop = rlayout.getBottom();//获取searchLayout的顶部位置
        }
    }

    //监听滚动Y值变化，通过addView和removeView来实现悬停效果
    @Override
    public void onScroll(int scrollY) {
        if (scrollY >= searchLayoutTop) {
            if (search_edit.getParent() != search01) {
                search02.removeView(search_edit);
                search01.addView(search_edit);
            }
        } else {
            if (search_edit.getParent() != search02) {
                search01.removeView(search_edit);
                search02.addView(search_edit);
            }
        }
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess(List<ObjectInfo> list) {
        if (list != null) {
            search_listView.setAdapter(null);
            Log.i("TAG", list.get(0).getDescribe().toString());
            search_listView.setAdapter(new MyAdapter(list, this));
            Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "无对应结果", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, "获取失败:" + error, Toast.LENGTH_SHORT).show();
    }
}
